import request from "@/utils/request";
const defaultApi = (apiName) => {
    return {
        list: (params) => request.get(`/${apiName}/list`, { params }),
        simpleList: (params) => request.get(`/${apiName}/simpleList`, { params }),
        page: (params) => request.get(`/${apiName}`, { params }),
        read: (id) => request.get(`/${apiName}/` + id),
        create: (id, data) => request.post(`/${apiName}`, data),
        update: (id, data) => request.put(`/${apiName}/` + id, { ...data, create_time: undefined, id: undefined }),
        delete: (id) => request.delete(`/${apiName}/` + id),
        bathDelete: (idList) => request.post(`/${apiName}/bathDelete`, { idList }),
    }
}
export const http = {
    login: (data) => request.post('/user/login', data),
    menu: defaultApi('menu'),
    role: defaultApi('role'),
    user: {
        ...defaultApi('user'),
        getUserInfo: (userId) => request.get(`/user/getUserInfo`, { params: { userId }}),
        getUserRoles: (userId) => request.get(`/user/getUserRoles`, { params: { userId }}),
        updateUserRoles: (data) => request.post(`/user/updateUserRoles`, data),
    },
    member: defaultApi('member'),
    banner: defaultApi('wxBanner'),
    category: defaultApi('wxMenu'),
    district: {
        ...defaultApi('district'),
        tree: (params) => request.get('/district/tree', { params })
    },
    officeBuilding: {
        ...defaultApi('officeBuilding'),
    },
    houseFollowup: {
        ...defaultApi('houseFollowup'),
    },
    officeBuildingTemplate: {
        ...defaultApi('officeBuildingTemplate'),
    },
    // Office shop related APIs
    officeCategory: {
        ...defaultApi('officeCategory'),
        tree: (params) => request.get('/officeCategory/tree', { params })
    },
    officeProduct: defaultApi('officeProduct'),
    officeOrder: {
        ...defaultApi('officeOrder'),
        ship: (data) => request.post('/officeOrder/ship', data),
        complete: (data) => request.post('/officeOrder/complete', data),
        cancel: (data) => request.post('/officeOrder/cancel', data),
        statistics: () => request.get('/officeOrder/statistics'),
        // WeChat payment endpoints
        createWxPayment: (data) => request.post('/officeOrder/createWxPayment', data),
        queryWxPayment: (orderId) => request.get('/officeOrder/queryWxPayment', { params: { id: orderId } }),
        refundOrder: (data) => request.post('/officeOrder/refundOrder', data),
        queryRefund: (refundId) => request.get('/officeOrder/queryRefund', { params: { id: refundId } }),
        listRefunds: (params) => request.get('/officeOrder/listRefunds', { params })
    },
    officeOrderRefund: {
        ...defaultApi('officeOrderRefund'),
        listByOrder: (orderId) => request.get('/officeOrderRefund/listByOrder', { params: { order_id: orderId } })
    },
    // 闲置物资设备平台相关API
    idleCategory: {
        ...defaultApi('idleCategory'),
        tree: (params) => request.get('/idleCategory/tree', { params }),
        getByType: (params) => request.get('/idleCategory/getByType', { params }),
        getSubTypes: (params) => request.get('/idleCategory/getSubTypes', { params })
    },
    idleItem: {
        ...defaultApi('idleItem'),
        nearby: (params) => request.get('/idleItem/nearby', { params }),
        incrementViewCount: (id) => request.post(`/idleItem/${id}/incrementViewCount`),
        batchImport: (data, params) => request.post(`/idleItem/batchImport?create_user_id=${params.create_user_id}&create_user_name=${params.create_user_name}`, data),
        statistics: (params) => request.get('/idleItem/statistics', { params }),
        myItems: (params) => request.get('/idleItem/myItems', { params })
    },
    idleTransfer: {
        ...defaultApi('idleTransfer'),
        apply: (data) => request.post('/idleTransfer/apply', data),
        approve: (id, data) => request.post(`/idleTransfer/${id}/approve`, data),
        complete: (id) => request.post(`/idleTransfer/${id}/complete`),
        cancel: (id) => request.post(`/idleTransfer/${id}/cancel`),
        myApplies: (params) => request.get('/idleTransfer/myApplies', { params }),
        myApprovals: (params) => request.get('/idleTransfer/myApprovals', { params }),
        statistics: (params) => request.get('/idleTransfer/statistics', { params })
    },
    idleFavorite: {
        ...defaultApi('idleFavorite'),
        toggle: (data) => request.post('/idleFavorite/toggle', data),
        myFavorites: (params) => request.get('/idleFavorite/myFavorites', { params }),
        check: (params) => request.get('/idleFavorite/check', { params })
    },
    policy:{
        ...defaultApi('policy'),
    }
}
// uploadFile
export const uploadFile = (file) => {
    const formData = new FormData()
    formData.append('file', file)
    return request({
        url: '/upload/file2',
        method: 'post',
        data: formData
    })
}
